package tech.aistar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import tech.aistar.entity.Flow;
import tech.aistar.entity.Myorder;
import tech.aistar.mapper.FlowMapper;
import tech.aistar.mapper.MyorderMapper;
import tech.aistar.service.IMyorderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 靳光乐
 * @since 2022-11-28
 */
@Service
public class MyorderServiceImpl extends ServiceImpl<MyorderMapper, Myorder> implements IMyorderService {

    @Autowired
    private MyorderMapper myorderMapper;

    @Autowired
    private FlowMapper flowMapper;
    @Override
    public boolean  updateStatus(Integer orderId, String tradeNo, BigDecimal payPrice) {
        //添加流水
        Flow flow =  new Flow();
        flow.setFlowNumber(tradeNo);
        flow.setOrderId(orderId);
        flow.setCustId(2);
        flow.setPayPrice(payPrice);
        flow.setPayTime(LocalDateTime.now());
        flowMapper.insert(flow);

//修改订单状态
        Myorder myorder = new Myorder();
        myorder.setOrderId(orderId);
        myorder.setStatus(2);
        int rows =  myorderMapper.updateById(myorder);
        return rows >0;
    }

    @Override
    public List<Myorder> getByCustomerId(Integer customerId) {
        QueryWrapper<Myorder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cust_id",customerId);
        return myorderMapper.selectList(queryWrapper);
    }
}
